package com.Ajava1704.for线程池;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * 作者: LCG
 * 日期: 2020/2/11 08:37
 * 描述: 讲解线程池
 */
public class ForXianChengchiTest {

    public static void main(String[] args) {

        LinkedBlockingQueue<Runnable> linkedBlockingQueue=new LinkedBlockingQueue<>();
        ForRejectHandler forRejectHandler=new ForRejectHandler(linkedBlockingQueue);


        ArrayBlockingQueue<Runnable> arrayBlockingQueue=new ArrayBlockingQueue<Runnable>(2);
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
                2,//核心线程数
                4,//最大线程数
                50,//线程的存活时间
                TimeUnit.SECONDS,//线程存活时间的单位
                arrayBlockingQueue //工作队列
                ,forRejectHandler
        );


        for (int i=0;i<7;i++){
            Runnable runnabl=new Runnable() {
                @Override
                public void run() {
                    try {
                        Thread.sleep(5000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println("===这是执行的业务代码===");
                }
            };
            threadPoolExecutor.execute(runnabl);
        }


        System.out.println(linkedBlockingQueue.size());

    }

}
